package com.chinasofti.controller;

import com.chinasofti.service.EmployeeService;
import com.chinasofti.vo.Employee;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String timelength = request.getParameter("timelength");

        //cookie
        int days = 0;
        if(timelength !=null){
            days = Integer.parseInt(timelength);
        }
        if(days != 0){
            Cookie usernamecookie = new Cookie("username",username);
            Cookie passwordcookie = new Cookie("pwd",pwd);
            usernamecookie.setMaxAge(days*24*3600);
            passwordcookie.setMaxAge(days*24*3600);
            response.addCookie(usernamecookie);
            response.addCookie(passwordcookie);
        }


        EmployeeService service = new EmployeeService();
        int flag = service.login(username, pwd);
        if (flag == 1){

            //添加访问登录统计
            ServletContext ctxt =   this.getServletContext();
            //判断 visitcount
            int visitcount;
            if(ctxt.getAttribute("visitcount") == null){
                visitcount = 0;
            }else {
                visitcount = Integer.parseInt(ctxt.getAttribute("visitcount").toString());
            }
            //将统计数字自增1，保存
            visitcount++;
            ctxt.setAttribute("visitcount",visitcount);

            //获取session
            HttpSession session = request.getSession();
            //获取对象
            Employee loginsemployee = service.getLoginsemployee();
            //信息存储到会话中
            session.setAttribute("empname",loginsemployee.getUsername());
            session.setAttribute("empid",loginsemployee.getEmployeeid());

            String role = loginsemployee.getRole();
            if (role.equals("1")){
                //管理员
                request.getRequestDispatcher("adminindex.jsp").forward(request,response);
            }
            if (role.equals("2")){
                //管理员
                request.getRequestDispatcher("empindex.jsp").forward(request,response);
            }

        }else {
            if (flag == 0){
                request.setAttribute("msg","账号处于审核状态，请稍后………");
            }
            if (flag == 2){
                request.setAttribute("msg","您注册的账号未通过管理员审核，请重新注册或联系管理员");
            }
            if (flag == 3 ){
                request.setAttribute("msg","您输入的账号名或密码有误");
            }
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }


    }
}
